
var findStuInfo = toContextPath('/student/student_getInfo.action');
var findAllExams = toContextPath('/student/exam_getAll.action');
var newexamHtml = toContextPath('/StuPages/NewExam.html');
var loginHtml = toContextPath('/StuPages/login.html');

var examState;
$(function(){	
	initPage();
});


function initPage() {
	location.hash = '';
	
	$.getJSON(findStuInfo, function(data) {
		if (data.result && data.result.stuName){
			$('#stuName').text(data.result.stuName);
		} else {
			window.location.href = loginHtml;
		}
	});
	
	$.getJSON(findAllExams, function(data) {
		if (data.result && data.result.length > 0){
			$.each(data.result, function(index, exam) {
				var item = '<tr';
				if (exam.state == 0) {
					item += ' class="warning">';
					examState = "Todo";
				} else if (exam.state == 1) {
					item += ' class="warning">';
					examState = "Doing";
				} else if (exam.state == 2) {
					item += ' class="success">';
					examState = "Done";
				}
				item +=   '<td>' + exam.examBeginDate + '</td>'
						+ '<td>' + exam.examEndDate	+ '</td>'
						+ '<td> <a onclick="clickA(' + exam.testId + ',' 
						+ exam.examId + ')">' + exam.examName+ '</a></td>'
						+ '<td>' + exam.teaName + '</td>'
						+ '<td';
				if (exam.state == 0 || 1) {
					item += ' style="color:red">' + examState + '</td>'+ '</tr>';
				} else if (exam.state == 2){
					item += ' style="color:green">' + examState + '</td>'+ '</tr>';
				}
				$('#examList').append(item);	
			});
		}
	});
	
}


function clickA(testId, examId) {
	//alert("You clicked the exam");
	if (examState == "Done" || examState == "Doing") {
		window.location.href = newexamHtml+'?testId='+ testId + '&examId=' + examId;
	} else if (examState == "Todo") {
		alert("The exam will start!");
	} 
}

//$("#userPwd").formValidator();